<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * category_model
 *  热门游戏管理模型
 */
class Hot_model extends CI_Model
{
    var $_table_name = 'game_hot';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param $page 分页开始数
     * @param $offset  分页数量
     * @param array $view 搜索关键词
     * @param  bool $is_new 是否查询最新
     * @return array
     */
    public function get_list($page, $offset, $view, $is_new = false)
    {
        $this->db->select('p.*,c.title as client_title');
        $this->db->from("$this->_table_name p");
        $this->db->join('client as c', 'c.id=p.client_id', 'left outer');
        if (!empty($view['title'])) {
            $this->db->like('p.title', $view['title']);
        }
        if (!empty($view['category_id'])) {
            $this->db->where('p.category_id', $view['category_id']);
        }
        if (!empty($view['client_id'])) {
            $this->db->where('p.client_id', $view['client_id']);
        }
        if (empty($view['admin'])) {
            $this->db->where('p.status', 0);
        }
        if ($is_new) {
            $this->db->order_by("p.createtime desc,p.click desc");
        } else {
            $this->db->order_by("p.sort desc,p.click desc,p.createtime desc");
        }

        $this->db->limit($offset, $page);
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * 获取总量
     * @param array $view 搜索关键词
     * @return array
     */
    public function total_rows($view)
    {
        $this->db->select('count(*) as num');
        $this->db->from("$this->_table_name");
        if (!empty($view['title'])) {
            $this->db->like('title', $view['title']);
        }
        if (!empty($view['category_id'])) {
            $this->db->where('category_id', $view['category_id']);
        }
        if (!empty($view['client_id'])) {
            $this->db->where('client_id', $view['client_id']);
        }
        if (empty($view['admin'])) {
            $this->db->where('p.status', 0);
        }
        $query = $this->db->get();
        $row = $query->row_array();
        return $row['num'];
    }

    /**
     * 批量更新游戏所属客户
     * @param array $arr_condition 游戏筛选条件关联数组（'字段'=>值）
     * @param int $new_client_id
     * @return mixed
     */
    public function batch_update_client($arr_condition, $new_client_id=1)
    {
        $res = '';
        if(is_array($arr_condition)){
            try{
                $res = $this->db->update($this->_table_name, array('client_id'=>$new_client_id), $arr_condition);
            }catch (Exception $e){
                log_message('error',$e->getCode().':'.$e->getMessage());
            }
        }

        if($res){
            return $this->db->affected_rows();//更新的游戏个数
        }else{
            return false;
        }
    }

    /**
     * 获取推荐的游戏列表
     * 推荐算法（暂时）：排除已经玩过，点击量降序排列
     * @param int $num 返回的游戏数量
     * @param array $exclude_ids 要排除的游戏id
     * @return array
     */
    public function get_recommend($num=4, $exclude_ids=array())
    {
        $this->db->select('p.*, c.title as category_title');
        $this->db->from("$this->_table_name p");
        $this->db->where('p.status', 0);
        $this->db->join('category as c', 'c.id=p.client_id', 'left outer');
        !empty($exclude_ids) && $this->db->where_not_in('p.id', $exclude_ids);
        $this->db->order_by("p.sort desc,p.click desc,p.createtime desc");

        $this->db->limit($num);
        $query = $this->db->get();
        return $query->result_array();
    }
}